<?php

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/cus.php';
if (file_exists($cron_lang))
{
    global $_LANG;

    include_once($cron_lang);
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'ciq_desc';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = '';

    /* 版本号 */
    $modules[$i]['version'] = '1.0.0';

    /* 配置信息 */
    $modules[$i]['config']  = array(
//         array('name' => 'ftp_host', 'type' => 'text', 'value' => ''),
//     	array('name' => 'ftp_port', 'type' => 'text', 'value' => '21'),
//     	array('name' => 'ftp_user', 'type' => 'text', 'value' => ''),
//     	array('name' => 'ftp_pass', 'type' => 'password', 'value' => ''),
//     	array('name' => 'local_path', 'type' => 'text', 'value' => '/cusxml/download/'),
//     	array('name' => 'remote_path', 'type' => 'text', 'value' => '')
    );

    return;
}
// if (empty($cron['ftp_host'])&&empty($cron['ftp_port'])&&empty($cron['ftp_user'])&&empty($cron['ftp_pass'])
// 		&&empty($cron['local_path'])&&empty($cron['remote_path']))
// {
// 	exit;
// }

include_once ROOT_PATH.'includes/cls_ftp.php';
$ftp = new cls_ftp(CIQ_HOST,CIQ_PORT,CIQ_USER,CIQ_PASS);
$directory = CIQ_REMOTE_DOWNLOAD;
$list = $ftp->get_list($directory);
if (empty($list)) exit;

foreach ($list as $k=>$v)
{
	$filename = basename($v);
	if (strpos($filename, 'xml') !== false)
	{
		$localpath = CIQ_LOCAL_DOWNLOAD.local_date('Ym');
		if (!file_exists($localpath)) mkdir($localpath);
		if ($ftp->down_file($localpath.'/'.$filename, $v))
		{
// 			$ftp->del_file($directory.'/'.$v);
			$obj = simplexml_load_file($localpath.'/'.$filename);
			print_r($obj);
			$order_sn = substr($obj->Declaration->OrgMessageID, 0, -4);
			$status = $obj->Declaration->Status;
			echo $order_sn;
			if ($order_sn)
			{
				$code = ($status==10)?1:0;
				$order_id = get_order_id_by_pp($order_sn);
				push_log($order_id, $code,$obj->Declaration->Notes,'推送国检', 0,0, 2);
				$ciq_status = ($status==10)?CIQ_PUSHED:CIQ_PUSHFAIL;
				$GLOBALS['db']->query("UPDATE ".$GLOBALS['ecs']->table('order_info')." SET ciq_status='$ciq_status' WHERE order_sn='$order_sn'");
			}
			
		}
		else 
		{
			echo 'down fail';
		}
	}
}


$ftp->close();

?>